package com.gg.star.contorller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.gg.entity.User;
import com.gg.star.service.StarService;

import helpers.baseservlet.BaseServlet;
import povos.StarFruitPovo;

/**
 * Servlet implementation class AddStarServlet
 */
@WebServlet("/star/AddStarServlet")
public class AddStarServlet extends BaseServlet {
	@Autowired
	private StarService starService;
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer fid=Integer.parseInt(request.getParameter("fid"));
		User user=(User) request.getSession().getAttribute("user");
		String   uid =user.getUid();
		StarFruitPovo povo=starService.getStarFruitPovoByUidAndFid(fid.toString(), uid);
		if(povo!=null) {
			response.sendRedirect("/fruits/showstar.jsp");
			return;
			
		}
		starService.addStar(uid,fid);
		List<StarFruitPovo> povos=starService.showAllStarsByUid(uid);
		request.getSession().setAttribute("povos",povos);
		response.sendRedirect("/fruits/showstar.jsp");
	}

}
